clear
clear all
cap log close

log using "log-files/3_Table_D1.smcl", replace smcl

timer on 2

*-----------*
* Table D.1 *
*-----------*
	use "data/outputs/computo_verificadortrepdate_missingsatthend.dta", clear
		
	quietly sum pcs_nbnn if trep_date_verificador >= tc(20oct2019 19:40:57) & ///
		trep_date_verificador <= tc(20oct2019 19:40:58), detail
	local cutoff = `r(min)' 
		
	* Post dummies
	gen pcs_centered = pcs_nbnn - `cutoff'
	gen post = pcs_nbnn >= `cutoff'
	gen postXpcs = post * pcs_centered	   
	gen margin_nbnn = mshare_nbnn - cshare_nbnn // Excluding blancos y nulos
	
	* Recinto ID
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)
		/* Note, in constructing the data set we made a correction
		such that recinto names are unique within localities. */

	eststo clear
	eststo: reg margin_nbnn pcs_centered post postXpcs, cl(reci_id)
	eststo: reghdfe margin_nbnn pcs_centered post postXpcs, a(reci_id) cl(reci_id)
	eststo: reghdfe margin_nbnn post, a(reci_id) cl(reci_id)
	eststo: reghdfe margin_nbnn post pcs_centered, a(reci_id) cl(reci_id)

	* Labelling
	label var pcs_centered "$\hat{\beta}_1$: Reporting time percentile$^\dagger$"
	label var post "$\hat{\beta}_2$: Post shutdown"
	label var postXpcs "$\hat{\beta}_3$: Percentile $\times$ Post"

	esttab using "outputs/generated/Table_D1.tex", ///
	       b(a2) replace alignment(S) substitute(\_ _) ///
		   keep(pcs_centered post postXpcs) ///
	       gaps compress se nostar ///
	       bookt fragment label nomtitles ///
		   nodepvars nonumbers nolines obslast ///
		   cells(b(fmt(3)) se(fmt(a1) par))

	* Additional formating
	insheet using "outputs/generated/Table_D1.tex", clear

	drop in 1 / 1

	replace v1 = "" if regexm(v1, "addlinespace")
	replace v1 = " " + v1 if regexm(v1, "\)&") 
	replace v1 = subinstr(v1, "Post shutdown", "Post shutdown (0/1)", .)
	replace v1 = subinstr(v1, "34", "34,", .) if regexm(v1, "Observations")
	replace v1 = subinstr(v1, "32", "32,", .) if regexm(v1, "Observations")
	replace v1 = "\midrule" if regexm(v1[_n + 1], "Observations")

	outsheet using "outputs/generated/Table_D1.tex", nonames noquote replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
